package com.wenruo.funycollecte.activity;

import com.wenruo.funycollecte.R;
import com.wenruo.funycollecte.fragment.ContentFragment;
import com.wenruo.funycollecte.fragment.ImageFragment;
import com.wenruo.funycollecte.util.Client;
import com.wenruo.funycollecte.util.ContextUtil;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

	@Override
	protected void onRestart() {
		super.onRestart();
		ContextUtil.init(getApplicationContext());
		Client.init();
		Log.i("activity lifecycle", "activity onRestart.");
	}

	SectionsPagerAdapter mSectionsPagerAdapter;
	ViewPager mViewPager;
	ContentFragment textFragment;
	ImageFragment imageFragment;
	ImageFragment baozouFragment;	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ContextUtil.init(getApplicationContext());
		Client.init();
		
		final ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
		
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});

		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			actionBar.addTab(actionBar.newTab()
					.setText(mSectionsPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_about) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		private String[] tabTitles = {"文字", "图片", "暴走"};
		
		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			if (position == 0) {
				if (textFragment == null) {
					Bundle arguments = new Bundle();
					arguments.putString("category", "xiaohua");
					arguments.putString("type", "text");
					arguments.putString("title", "wenzi");
					textFragment = new ContentFragment();
					textFragment.setArguments(arguments);
				} 
				return textFragment;
			} else if (position == 1) {
				if (imageFragment == null) {
					Bundle arguments = new Bundle();
					arguments.putString("category", "xiaohua");
					arguments.putString("type", "image");
					arguments.putString("title", "tupian");
					imageFragment = new ImageFragment();
					imageFragment.setArguments(arguments);
				}
				return imageFragment;
			} else {
				if (baozouFragment == null) {
					Bundle arguments = new Bundle();
					arguments.putString("category", "baozoumanhua");
					arguments.putString("type", "image");
					arguments.putString("title", "baozou");
					baozouFragment = new ImageFragment();
					baozouFragment.setArguments(arguments);
				}
				return baozouFragment;
			}
		}

		@Override
		public int getCount() {
			return tabTitles.length;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return tabTitles[position];
		}
	}
}
